package utils;

import com.opencsv.CSVReader;

import java.io.*;
import java.util.*;

/**
 * @projectName: bigScaleLoadTest
 * @className: PareLog
 * @description:
 * @author: JLONG
 * @date: 2024/11/7 14:39
 * @version: 1.0
 */
public class LogParser {
    public static void main(String[] args) {
        getSuccessRate("/Users/jlong/IdeaProjects/bigScaleLoadTest/BSL-ImageLoad_pan_3.1.6_pvt_chan__24-11-07_171450.csv");
    }

    public static void getSuccessRate(String logFilePath){
        int counter_total = 0;
        int counter_success = 0;
        int counter_error = 0;
        int counter_fail = 0;
        Map<String, Integer> labelListWithFailedTask = new HashMap<>();
        List<String> failedLabels = new ArrayList<>();
        List<String> errorLabels = new ArrayList<>();
        Map<String, Integer>  labelListWithErrorTask = new HashMap<>();
        File inputFile = new File(logFilePath);
        CSVReader reader = null;
        try {
            reader = new CSVReader(new FileReader(inputFile));
            List<String[]> records = reader.readAll();
            for (String[] record : records) {
                    if (record[1].equals("TASK_FINISHED_EVENT") && record[5].equals("IMAGE")) {
                        counter_total++;
                        if (record[9].equals("SUCCESSFUL")) {
                            counter_success++;
                        } else if(record[9].equals("ERROR")){
                            counter_error++;
                            updateMap(labelListWithErrorTask, record[6]);
                            errorLabels.add(record[6]);
                        } else if(record[9].equals("FAILED")){
                            counter_fail++;
                            updateMap(labelListWithFailedTask, record[6]);
                            failedLabels.add(record[6]);
                        }
                    }
                }
            reader.close();
            saveDataToFile("labelsWithErrorTask(重复ID).csv", errorLabels);
            saveDataToFile("labelsWithErrorTask.csv", labelListWithErrorTask);
            saveDataToFile("labelsWithFailedTask.csv", labelListWithFailedTask);
            saveDataToFile("labelsWithFailedTask(重复ID）.csv",failedLabels);
            System.out.println(
                    "Total task:    " + counter_total + "\n" +
                    "Success task:  " + counter_success + "\n" +
                    "Failed task:   " + counter_fail + "\n" +
                    "Error task:    " + counter_error + "\n" +
                    "Success rate:  " + String.format("%.2f%%", (double)counter_success/counter_total*100));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    public static void updateMap(Map<String, Integer> labelMap, String labelID){
        if(labelMap.containsKey(labelID)){
            int oldValue = labelMap.get(labelID);
            labelMap.put(labelID, oldValue + 1);
        } else{
            labelMap.put(labelID, 1);
        }

    }

    public static void saveDataToFile(String filePath, Map<String, Integer> labelMap){

        // 使用 BufferedWriter 将内容写入文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            // 遍历 Set 并写入文件
            for (String item : labelMap.keySet()) {
                writer.write(item + "," + labelMap.get(item));
                writer.newLine(); // 写入新的一行
            }
            System.out.println("Set contents written to file successfully.");
        } catch (IOException e) {
            System.err.println("Error writing to file: " + e.getMessage());
        }
    }

    public static void saveDataToFile(String filePath, List<String> labelList){
        // 使用 BufferedWriter 将内容写入文件
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            // 遍历 Set 并写入文件
            for (String item : labelList) {
                writer.write(item);
                writer.newLine(); // 写入新的一行
            }
            System.out.println("Set contents written to file successfully.");
        } catch (IOException e) {
            System.err.println("Error writing to file: " + e.getMessage());
        }
    }
}